package xyz.jcat.common.util;

import org.apache.commons.lang3.time.DateFormatUtils;
import xyz.jcat.common.exception.ConvertException;

import java.text.ParseException;
import java.util.Date;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";
    public static final String FORMAT_YYYYMMDD = "yyyyMMdd";

    public static String format(Date date) {
        return format(date, DEFAULT_FORMAT);
    }

    public static String format(Date date, String format) {
        return DateFormatUtils.format(date, format);
    }

    public static String toCron(Date date) {
        return format(date, CRON_DATE_FORMAT);
    }

    public static Date parseDate(String dateStr) {
        try {
            return parseDate(dateStr, DEFAULT_FORMAT);
        } catch (ParseException e) {
            throw new ConvertException("字符串转日期异常", e);
        }
    }

}
